/*
 * Copyright 2019 Zhenjie Yan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.chushizhang;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.definerecycleview.ExpandableAdapter;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.dialog.ButtomDialogView;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.ChushiShenHeModel;
import com.mingmen.canting.model.DingDanZhongLeiModel;
import com.mingmen.canting.model.Group;
import com.mingmen.canting.model.GroupMember;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.List;

/**
 * Created by Zhenjie Yan on 1/30/19.
 */
public class ChuShiDingZhongLeiAdapter extends ExpandableAdapter<ExpandableAdapter.ViewHolder> {

    private LayoutInflater mInflater;
    private List<DingDanZhongLeiModel.DataBean> mGroupList;
    Context context;

    String token;
    int checkedPosition;
    List<DingDanZhongLeiModel.DataBean.DetailsBean> memberList;
    int flag = 0;

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void itemClickListener(View view, int parentPosition, int position, List<DingDanZhongLeiModel.DataBean> groupList, List<DingDanZhongLeiModel.DataBean.DetailsBean> memberList);
    }

    private OnItemClickListener mOnItemClickListener;


    public void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public ChuShiDingZhongLeiAdapter(Context context, List<DingDanZhongLeiModel.DataBean> mGroupList, int flag) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context.getApplicationContext();
        this.mGroupList = mGroupList;
        this.flag = flag;
        token = (String) SharePreferenceUtil.get(context, "token", "");

    }


    public void setGroupList(List<DingDanZhongLeiModel.DataBean> groupList) {
        this.mGroupList = groupList;
        notifyDataSetChanged();
    }

    @Override
    public int parentItemCount() {
        return mGroupList == null ? 0 : mGroupList.size();
    }

    @Override
    public int childItemCount(int parentPosition) {
//        List<ChushiShenHeModel.DataBean.DetailsBean> memberList = mGroupList.get(parentPosition).getMemberList();
        memberList = mGroupList.get(parentPosition).getDetails();
        return memberList == null ? 0 : memberList.size();
    }

    @Override
    public ViewHolder createParentHolder(@NonNull ViewGroup root, int viewType) {
        View view = mInflater.inflate(R.layout.item_chushidai_expand_parent, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_test, root, false);

        return new ParentHolder(view, this);
    }

    @Override
    public ViewHolder createChildHolder(@NonNull ViewGroup root, int viewType) {
//        View view = mInflater.inflate(R.layout.item_chushi_shenhe_child, root, false);
        View view = mInflater.inflate(R.layout.item_expand_chushi, root, false);
//        View view = mInflater.inflate(R.layout.item_expand_child, root, false);
        return new ChildHolder(view, this);
    }

    @Override
    public void bindParentHolder(@NonNull ViewHolder holder, int position) {
//        ((ParentHolder) holder).setData(mGroupList.get(position));
        DingDanZhongLeiModel.DataBean data = mGroupList.get(position);
        ParentHolder parentHolder = (ParentHolder) holder;
        parentHolder.mTvTitle.setText(data.getCommodityName());
        parentHolder.limitDate.setVisibility(View.GONE);
        if (flag == 1) {
            parentHolder.limitDate.setVisibility(View.GONE);
        } else {
//            parentHolder.limitDate.setVisibility(View.VISIBLE);
        }

//        layout_parent
        parentHolder.mIvStatus.setSelected(data.isExpanded());
        if (data.isExpanded() == true) {
            parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_item2));
        } else {
            parentHolder.layout_parent.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem1));
        }
        if (position == 0) {

            parentHolder.layout_view.setVisibility(View.GONE);
        } else {

            parentHolder.layout_view.setVisibility(View.VISIBLE);
        }
        if (data.isExpanded() == true) {
            Matrix matrix = new Matrix();
            Bitmap bitmap = ((BitmapDrawable) context.getResources().getDrawable(R.mipmap.arrow_right_black)).getBitmap();
// 设置旋转角度
            matrix.setRotate(90);
// 重新绘制Bitmap
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            parentHolder.mIvStatus.setImageBitmap(bitmap);
        } else {
            parentHolder.mIvStatus.setImageResource(R.mipmap.arrow_right_black);
        }


    }

    public void setCheckedPosition(int checkedPosition) {
        this.checkedPosition = checkedPosition;
        notifyDataSetChanged();
    }

    @Override
    public void bindChildHolder(@NonNull ViewHolder holder, int parentPosition, int position) {
//        ((ChildHolder)holder).setData(mGroupList.get(parentPosition).getMemberList().get(position));
        ChildHolder myholder = (ChildHolder) holder;
//        myholder.setData(mGroupList.get(parentPosition).getMemberList().get(position));
        mGroupList.get(parentPosition).isExpanded();
        int childsize = mGroupList.get(parentPosition).getDetails().size();
//        shape_chushi_dingdan_ziitem3
        if (position == childsize - 1) {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem2));
        } else {
            myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        }
//        myholder.layout_item.setBackground(context.getResources().getDrawable(R.drawable.shape_chushi_dingdan_ziitem3));
        DingDanZhongLeiModel.DataBean.DetailsBean data = mGroupList.get(parentPosition).getDetails().get(position);
        myholder.mTvTitle.setText(data.getAuditUser());

        myholder.commodityName.setText(data.getCommodityName());
        myholder.applyAmount.setText("数量:" + data.getApplyAmount() + "");
        myholder.quotedPrice.setText(data.getQuotedPrice());
        myholder.text_mark.setText("备注:" + data.getMark());
        myholder.text_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* */
                mOnItemClickListener.itemClickListener(v, parentPosition, position, mGroupList, memberList);
            }
        });
        myholder.layout_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* */
                mOnItemClickListener.itemClickListener(v, parentPosition, position, mGroupList, memberList);
            }
        });
        myholder.layout_id.setVisibility(View.VISIBLE);
        Glide.with(context).load(AppConfig.IMGURL + data.getPhoto()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(myholder.ivAvatar);
    }

    static class ParentHolder extends ViewHolder {

        TextView mTvTitle;
        ImageView mIvStatus;
        LinearLayout layout_parent;
        TextView limitDate;
        View layout_view;

        public ParentHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            mIvStatus = itemView.findViewById(R.id.iv_status);
            layout_parent = itemView.findViewById(R.id.layout_parent);
            limitDate = itemView.findViewById(R.id.limitDate);
            layout_view = itemView.findViewById(R.id.layout_view);
        }

        public void setData(Group data) {
            mTvTitle.setText(data.getName());
            mIvStatus.setSelected(data.isExpanded());
        }
    }

    static class ChildHolder extends ViewHolder {

        TextView mTvTitle;
        LinearLayout layout_id;
        LinearLayout layout_item;
        TextView commodityName;
        TextView applyAmount;
        TextView quotedPrice;
        TextView text_mark;
        TextView text_cancel;

        ImageView ivAvatar;

        public ChildHolder(@NonNull View itemView, ExpandableAdapter adapter) {
            super(itemView, adapter);
            mTvTitle = itemView.findViewById(R.id.tv_title);
            layout_id = itemView.findViewById(R.id.layout_id);
            layout_item = itemView.findViewById(R.id.layout_item);
            commodityName = itemView.findViewById(R.id.commodityName);
            applyAmount = itemView.findViewById(R.id.applyAmount);
            quotedPrice = itemView.findViewById(R.id.quotedPrice);
            text_mark = itemView.findViewById(R.id.text_mark);
            text_cancel = itemView.findViewById(R.id.text_cancel);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
        }

        public void setData(GroupMember data) {
            mTvTitle.setText(data.getName());
        }
    }


}